/*
 * User: ronma
 * Date: 15/05/2008
 *
 * Copyright (2005) IDI. All rights reserved.
 * This software is a proprietary information of Israeli Direct Insurance.
 * Created by IntelliJ IDEA.
 */
package com.idi.maven.plugins;

import java.io.*;
import static java.lang.String.format;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 *
 */
public class BuildPropertiesFactory {
    private Map<String, String> props = new HashMap<String, String>(128);

    private static final String SERVERS_S_PROPERTIES = "servers/%s.properties";
    private static final String WEB_SERVERS_S_PROPERTIES = "web_servers/%s_web.properties";

    private BuildPropertiesFactory() {
    }

    @SuppressWarnings({"AccessingNonPublicFieldOfAnotherObject"})
    public static BuildProperties createBuildPropertiesForJndiServer(String jndiServer) {
        BuildPropertiesFactory factory = initBuildProperties(jndiServer);
        return new BuildProperties(factory.props);
    }

    @SuppressWarnings({"AccessingNonPublicFieldOfAnotherObject"})
    public static BareketBuildProperties createBuildPropertiesForWebServer(String jndiServer) {
        BuildPropertiesFactory factory = initWebBuildProperties(jndiServer);
        return new BareketBuildProperties(factory.props);
    }

    private static BuildPropertiesFactory initBuildProperties(String jndiServer) {
        return new BuildPropertiesFactory()
                .addProperties(format(SERVERS_S_PROPERTIES, jndiServer))
                .addProperties("build.properties")
                .addProperties(System.getProperties());
    }

    private static BuildPropertiesFactory initWebBuildProperties(String jndiServer) {
        return new BuildPropertiesFactory()
                .addProperties(format(WEB_SERVERS_S_PROPERTIES, jndiServer))
                .addProperties("build.properties")
                .addProperties(System.getProperties());
    }

    public BuildPropertiesFactory addProperties(File propertiesFile) {
        FileInputStream stream = null;
        try {
            stream = new FileInputStream(propertiesFile);
            addProperties(getProperties(stream));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(format("could not find properties file %s", propertiesFile.getAbsolutePath()), e);
        } finally {
            try {
                //noinspection ConstantConditions
                stream.close();
            } catch (Exception e) {
                throw new RuntimeException(format("could not close stream %s", propertiesFile.getAbsolutePath()), e);
            }
        }
        return this;
    }

    public BuildPropertiesFactory addProperties(Properties properties) {
        for (Object o : properties.keySet()) {
            String key = (String) o;
            String value = properties.getProperty(key);
            props.put(key, value);
        }

        return this;
    }

    @SuppressWarnings({"ConstantConditions"})
    public BuildPropertiesFactory addProperties(String resource) {
        InputStream is = null;
        try {
            is = getClass().getClassLoader().getResourceAsStream(resource);
            if (is == null) {
                throw new RuntimeException(format("could not find resource %s", resource));
            }
            addProperties(getProperties(is));
        }
        finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (Exception e) {
                throw new RuntimeException(format("could not close resource %s", resource), e);
            }
        }
        return this;
    }

    private static Properties getProperties(InputStream stream) {
        try {
            Properties properties = new Properties();
            properties.load(stream);
            return properties;
        } catch (IOException e) {
            throw new RuntimeException(format("could not load properties from stream %s", stream), e);
        }
    }

}